
const { register, login, update, queryAdmin, deleteAdmin } = require('../../../controller/system/user/USER.CONTROLLER')
const { serverError } = require('../../../utils')
const { emptyCheck } = require('../../../utils/checkEmpty')
const pageSizeList = require('../../../utils/slicePageSize')


const router = require('koa-router')()

router.prefix('/user')

router.get('/', function (ctx, next) {
  ctx.body = 'this is a users response!'
})



//删除成员
router.post("/delete", async (ctx, next) => {
  try {
    const { ids = [] } = ctx.request.body;
    if (ids.length === 0) {
      ctx.body = {
        status: '0011',
        message: "未选择需要删除的数据"
      }
    }
    else {
      const res = await deleteAdmin({ ids: ids })
      ctx.body = res;
    }
  } catch (error) {
    console.log("删除接口报错：", error);
    ctx.body = serverError
  }
})


// 用户注册
router.post("/register", async (ctx, next) => {
  try {
    const { roleId, roleName, userId, passWord, userName, cost, imgUrl } = ctx.request.body;
    const check = emptyCheck({ userId, passWord })
    if (check.code!== 0) {
      ctx.body = check;
    } else {
      const res = await register({ roleId, roleName, userId, userName, passWord, cost, imgUrl })
      ctx.body = res
    }
  } catch (error) {
    ctx.body = {
      status: "1000",
      message: "服务器故障",
    }
  }
})

// 更新用户信息
router.post("/updateInfo", async (ctx, next) => {
  try {
    const { userId, _id, userName } = ctx.request.body
    const check = emptyCheck({ userId, _id, userName })
    if (check.code!== 0) {
      ctx.body = check
    } else {
      const res = await update({ userId, _id, params: { userName } })
      ctx.body = res;
    }
  } catch (error) {
    ctx.body = {
      status: "1000",
      message: "服务器故障",
    }
  }
})

// 获取当前用户的菜单权限 /admin_users-api/getUserMenuAuthCode
router.post("/getUserMenuAuthCode", async (ctx, next) => {
  ctx.body = {
    status: "0000",
    message: "权限查询成功",
    data: {
      keyCodeList: ['010000', "100000", "100001", "100002", "200002", '200000', '020300', '300001', '300000', '300002', '400000', '400001']
    }
  }
})




//get list
router.post("/list", async (ctx, next) => {
  try {
    const { userId, roleName, roleId, userName, current, pageSize, status } = ctx.request.body;
    const res = await queryAdmin({
      userId, userName, roleName, roleId, status, current, pageSize,
    })
    ctx.body =res
  } catch (error) {
    ctx.body = serverError
  }
})



//获取员工详情
router.post("/getDetail", async (ctx, next) => {
  try {
    const { userId, _id } = ctx.request.body;
    const res = await queryAdmin({
      userId,
    })
    ctx.body = {
      status: "0000",
      message: "查询成功",
      data: {
        total: res.length,
        list: pageSizeList({ data: res, pageSize: 10, current: 1 })
      }
    }
  } catch (error) {
    console.log(error)
    ctx.body = serverError;
  }
})


module.exports = router
